package com.dzt.androidframework.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;


import com.dzt.androidframework.R;


import java.util.List;

/**
 * 这个用RadioGroup代替FragmentTabHost的实现，解决fragment状态不保存的问题
 */
public class FragmentTabAdapter implements RadioGroup.OnCheckedChangeListener {
	private List<FragmentBase> mFragments; // 一个tab页面对应一个Fragment
	private RadioGroup mRadioGroup; // 用于切换tab
	private FragmentManager mFragmentManager; // Fragment所属的Activity
	private int mFragmentContentId; // Activity中所要被替换的区域的id

	private int mCurrentTabIndex = 0; // 当前Tab页面索引

	private OnExtraCheckedChangedListener mExtraCheckedChangedListener; // 用于让调用者在切换tab时候增加新的功能

	public FragmentTabAdapter(FragmentManager frgmanager, List<FragmentBase> fragments,
							  int fragmentContentId, RadioGroup rgs) {
		this.mFragments = fragments;
		this.mRadioGroup = rgs;
		this.mFragmentManager = frgmanager;
		this.mFragmentContentId = fragmentContentId;

		// 默认显示第一页
		if (!(mFragments == null || mFragments.isEmpty())) {
			final Fragment fragment = fragments.get(0);
			if (!fragment.isAdded()) {
				FragmentTransaction ft = mFragmentManager.beginTransaction();
				ft.add(fragmentContentId, fragment);
				ft.commitAllowingStateLoss();
			}
		}

		rgs.setOnCheckedChangeListener(this);
	}

	@Override
	public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

		for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
			if (mRadioGroup.getChildAt(i).getId() == checkedId) {
				FragmentTransaction ft = obtainFragmentTransaction(i);
				hideCurrentFragment(ft);// 隐藏当前fragment
				showCheckedFragment(ft, i);// 显示当前checked fragment
				ft.commitAllowingStateLoss();

				mCurrentTabIndex = i; // 更新目标tab为当前tab

				// 如果设置了切换tab额外功能功能接口
				if (null != mExtraCheckedChangedListener) {
					mExtraCheckedChangedListener.onExtraCheckedChanged(radioGroup, checkedId, i);
				}

			}
		}

	}

	/**
	 * 隐藏当前的fragment
	 */
	private void hideCurrentFragment(FragmentTransaction ft) {
		Fragment fragment = getFragmentByPosition(mCurrentTabIndex);// 目前当前的tab
		if (fragment != null) {
			ft.hide(fragment); // 隐藏当前tab
			fragment.setUserVisibleHint(false);
		}
	}

	/**
	 * 显示选中的fragment
	 */
	private void showCheckedFragment(FragmentTransaction ft, int position) {
		Fragment fragment = getFragmentByPosition(position);// 目标tab
		if (fragment != null) {
			if (fragment.isAdded()) {
				ft.show(fragment); // 显示目标tab
			} else {
				ft.add(mFragmentContentId, fragment);// 添加当前tab
			}
			fragment.setUserVisibleHint(true);
		}
	}

	/**
	 * 获取一个带动画的FragmentTransaction
	 */
	private FragmentTransaction obtainFragmentTransaction(int index) {
		FragmentTransaction ft = mFragmentManager.beginTransaction();
		// 设置切换动画
		if (index > mCurrentTabIndex) {
			ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
		} else {
			ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_right_out);
		}
		return ft;
	}

	public int getCurrentTabIndex() {
		return mCurrentTabIndex;
	}

	public FragmentBase getFragmentByPosition(int index) {
		return mFragments == null ? null : (index < 0 || index >= mFragments.size() ? null
				: mFragments.get(index));
	}

	public void setOnRgsExtraCheckedChangedListener(OnExtraCheckedChangedListener listener) {
		this.mExtraCheckedChangedListener = listener;
	}

	/**
	 * 切换tab额外功能功能接口
	 */
	public interface OnExtraCheckedChangedListener {
		void onExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index);
	}

}
